package com.lys.service.impl;

import com.lys.dao.NewsDao;
import com.lys.entity.News;
import com.lys.param.NewsParams;
import com.lys.service.NewsService;
import com.lys.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class NewsServiceImpl implements NewsService {

	@Autowired
	private  NewsDao newsDao;
	/**
	 * 保存新闻
	 * @param news
	 */
	@Override
	public void saveNews(News news) {
		try {
			newsDao.save(news);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据id查询新闻
	 * @param id
	 * @return
	 */
	@Override
	public News findNewsById(String id) {
		News news = null;
		try {
			news = newsDao.getNewsById(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return news;
	}

	/**
	 * 获取所有新闻
	 * @param pager
	 * @return
	 */
	@Override
	public List<News> getAllNews(Pager pager) {
		List<News> news = null;
		try {
			NewsParams params = new NewsParams();
			// currentPage 当前页面, rowPerPage 每页显示条数
			// 第一个参数 起始页，第二个参数 每页显示几条
			params.openPage((pager.getCurrentPage() - 1) * pager.getRowPerPage(),
					pager.getRowPerPage());
			news = newsDao.queryNewsList(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return news;
	}

	/**
	 * 删除新闻
	 * @param id
	 */
	@Override
	public void deleteNews(String id) {
		try {
			newsDao.deleteById(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更新新闻
	 * @param news
	 */
	public void updateNews(News news) {
		try {
			newsDao.update(news);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<News> queryNewsList(NewsParams param) {
		List<News> news = null;

		try {
			news = newsDao.queryNewsList(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return news;
	}

	@Override
	public Integer queryNewsCount(NewsParams param) {
		Integer count = 0;

		try {
			count =  newsDao.queryNewsCount(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 根据标题
	 * ...
	 * 模糊查询
	 * @param param
	 * @return
	 */
	public List<News> queryNewsPageList(NewsParams param) throws Exception {
		List<News> news = null;
		news = newsDao.queryNewsList(param);
		return news;
	}

}
